🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / feature / messaging / src / commonMain / kotlin / org / meshtastic / feature / messaging / ui / contact / ContactsViewModel.kt
Displaying Raw • Download
feature/messaging/src/commonMain/kotlin/org/meshtastic/feature/messaging/ui/contact/ContactsViewModel.kt fix/fdroid-map-rendering (113121c4) Text, 8.61 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.feature.messaging.ui.contact
Tff7b72import T7ee787androidx.lifecycle.SavedStateHandle
Tff7b72import T7ee787androidx.lifecycle.ViewModel
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.StateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.combine
Tff7b72import T7ee787kotlinx.coroutines.flow.map
Tff7b72import T7ee787org.koin.core.annotation.KoinViewModel
Tff7b72import T7ee787org.meshtastic.core.common.util.ioDispatcher
Tff7b72import T7ee787org.meshtastic.core.model.Contact
Tff7b72import T7ee787org.meshtastic.core.model.ContactKey
Tff7b72import T7ee787org.meshtastic.core.model.DataPacket
Tff7b72import T7ee787org.meshtastic.core.model.MyNodeInfo
Tff7b72import T7ee787org.meshtastic.core.model.NodeAddress
Tff7b72import T7ee787org.meshtastic.core.model.isBroadcast
Tff7b72import T7ee787org.meshtastic.core.model.isFromLocal
Tff7b72import T7ee787org.meshtastic.core.model.util.getChannel
Tff7b72import T7ee787org.meshtastic.core.repository.ConnectionStateProvider
Tff7b72import T7ee787org.meshtastic.core.repository.NodeRepository
Tff7b72import T7ee787org.meshtastic.core.repository.PacketRepository
Tff7b72import T7ee787org.meshtastic.core.repository.RadioConfigRepository
Tff7b72import T7ee787org.meshtastic.core.ui.viewmodel.safeLaunch
Tff7b72import T7ee787org.meshtastic.core.ui.viewmodel.stateInWhileSubscribed
Tff7b72import T7ee787org.meshtastic.proto.ChannelSet
Tff7b72import T7ee787kotlin.collections.map Tff7b72as Te6edf3collectionsMap
Tf0883e@KoinViewModel
Tff7b72class T56d364ContactsViewModelTb4b4b4(
Tff7b72private Tff7b72val Te6edf3savedStateHandleTb4b4b4: Te6edf3SavedStateHandleTb4b4b4,
Tff7b72private Tff7b72val Te6edf3nodeRepositoryTb4b4b4: Te6edf3NodeRepositoryTb4b4b4,
Tff7b72private Tff7b72val Te6edf3packetRepositoryTb4b4b4: Te6edf3PacketRepositoryTb4b4b4,
Te6edf3radioConfigRepositoryTb4b4b4: Te6edf3RadioConfigRepositoryTb4b4b4,
Te6edf3connectionStateProviderTb4b4b4: Te6edf3ConnectionStateProviderTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3ViewModelTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3ourNodeInfo Tff7b72= Te6edf3nodeRepositoryTb4b4b4.Te6edf3ourNodeInfo
Tff7b72private Tff7b72val Te6edf3collapsedSectionsCsv Tff7b72= Te6edf3savedStateHandleTb4b4b4.Te6edf3getStateFlowTb4b4b4(Te6edf3KEY_COLLAPSED_SECTIONSTb4b4b4, Ta5d6ff"Ta5d6ff"Tb4b4b4)
T8b949e/** Collapsed conversation-list section keys (see [ContactSection]), persisted across process death. */
Tff7b72val Te6edf3collapsedSectionsTb4b4b4: Te6edf3StateFlowTff7b72<Te6edf3SetTff7b72<Tffa657StringTff7b72>Tff7b72> Tff7b72=
Te6edf3collapsedSectionsCsvTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3toSectionSetTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3stateInWhileSubscribedTb4b4b4(Te6edf3initialValue Tff7b72= Te6edf3emptySetTb4b4b4(Tb4b4b4)Tb4b4b4)
Tff7b72fun Td2a8fftoggleSectionCollapseTb4b4b4(Te6edf3sectionKeyTb4b4b4: Tffa657StringTb4b4b4) Tb4b4b4{
T8b949e// Read the authoritative saved value, not collapsedSections.value (which is emptySet when unsubscribed).
Tff7b72val Te6edf3current Tff7b72= Te6edf3collapsedSectionsCsvTb4b4b4.Te6edf3valueTb4b4b4.Te6edf3toSectionSetTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3updated Tff7b72= Tff7b72if Tb4b4b4(Te6edf3sectionKey Tff7b72in Te6edf3currentTb4b4b4) Te6edf3current Tff7b72- Te6edf3sectionKey Tff7b72else Te6edf3current Tff7b72+ Te6edf3sectionKey
Te6edf3savedStateHandleTff7b72[Te6edf3KEY_COLLAPSED_SECTIONSTff7b72] Tff7b72= Te6edf3updatedTb4b4b4.Te6edf3joinToStringTb4b4b4(Ta5d6ff"Ta5d6ff,Ta5d6ff"Tb4b4b4)
Tb4b4b4}
Tff7b72val Te6edf3connectionState Tff7b72= Te6edf3connectionStateProviderTb4b4b4.Te6edf3connectionState
Tff7b72val Te6edf3unreadCountTotal Tff7b72= Te6edf3packetRepositoryTb4b4b4.Te6edf3getUnreadCountTotalTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3stateInWhileSubscribedTb4b4b4(T79c0ff0Tb4b4b4)
Tff7b72val Te6edf3channels Tff7b72= Te6edf3radioConfigRepositoryTb4b4b4.Te6edf3channelSetFlowTb4b4b4.Te6edf3stateInWhileSubscribedTb4b4b4(Te6edf3initialValue Tff7b72= Te6edf3ChannelSetTb4b4b4(Tb4b4b4)Tb4b4b4)
T8b949e// Combine node info and myId to reduce argument count in subsequent combines
Tff7b72private Tff7b72val Te6edf3identityFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3PairTff7b72<Te6edf3MyNodeInfo?Tb4b4b4, Tffa657String?Tff7b72>Tff7b72> Tff7b72=
Te6edf3combineTb4b4b4(Te6edf3nodeRepositoryTb4b4b4.Te6edf3myNodeInfoTb4b4b4, Te6edf3nodeRepositoryTb4b4b4.Te6edf3myIdTb4b4b4) Tb4b4b4{ Te6edf3infoTb4b4b4, Te6edf3id Tff7b72-Tff7b72> Te6edf3PairTb4b4b4(Te6edf3infoTb4b4b4, Te6edf3idTb4b4b4) Tb4b4b4}
T8b949e/**
* Flat contact list (channels + DMs) with empty-channel placeholders merged in, consumed by both ContactsScreen and
* ShareScreen. ContactsScreen groups it into collapsible Channels/DirectMessages sections at render time.
*/
Tff7b72val Te6edf3contactList Tff7b72=
Te6edf3combineTb4b4b4(Te6edf3identityFlowTb4b4b4, Te6edf3packetRepositoryTb4b4b4.Te6edf3getContactsTb4b4b4(Tb4b4b4)Tb4b4b4, Te6edf3channelsTb4b4b4, Te6edf3packetRepositoryTb4b4b4.Te6edf3getContactSettingsTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3identityTb4b4b4,
Te6edf3contactsTb4b4b4,
Te6edf3channelSetTb4b4b4,
Te6edf3settingsTb4b4b4,
Tff7b72-Tff7b72>
Tff7b72val Tb4b4b4(Te6edf3myNodeInfoTb4b4b4, Te6edf3myIdTb4b4b4) Tff7b72= Te6edf3identity
Tff7b72val Te6edf3myNodeNum Tff7b72= Te6edf3myNodeInfoTff7b72?.Te6edf3myNodeNum Tff7b72?: Tff7b72returnTf0883e@combine Te6edf3emptyListTff7b72<Te6edf3ContactTff7b72>Tb4b4b4(Tb4b4b4)
T8b949e// Add empty channel placeholders (always show Broadcast contacts, even when empty)
Tff7b72val Te6edf3placeholder Tff7b72=
Tb4b4b4(T79c0ff0 Te6edf3until Te6edf3channelSetTb4b4b4.Te6edf3settingsTb4b4b4.Te6edf3sizeTb4b4b4)Tb4b4b4.Te6edf3associate Tb4b4b4{ Te6edf3ch Tff7b72-Tff7b72>
Tff7b72val Te6edf3contactKey Tff7b72= Te6edf3ContactKeyTb4b4b4.Te6edf3broadcastTb4b4b4(Te6edf3chTb4b4b4)Tb4b4b4.Te6edf3value
Tff7b72val Te6edf3data Tff7b72= Te6edf3DataPacketTb4b4b4(Te6edf3bytes Tff7b72= Tff7b72nullTb4b4b4, Te6edf3dataType Tff7b72= T79c0ff1Tb4b4b4, Te6edf3time Tff7b72= T79c0ff0LTb4b4b4, Te6edf3channel Tff7b72= Te6edf3chTb4b4b4)
Te6edf3contactKey Te6edf3to Te6edf3data
Tb4b4b4}
Tb4b4b4(Te6edf3contacts Tff7b72+ Tb4b4b4(Te6edf3placeholder Tff7b72- Te6edf3contactsTb4b4b4.Te6edf3keysTb4b4b4)Tb4b4b4)Tb4b4b4.Te6edf3entriesTb4b4b4.Te6edf3collectionsMap Tb4b4b4{ Te6edf3entry Tff7b72-Tff7b72>
Tff7b72val Te6edf3contactKey Tff7b72= Te6edf3entryTb4b4b4.Te6edf3key
Tff7b72val Te6edf3packetData Tff7b72= Te6edf3entryTb4b4b4.Te6edf3value
T8b949e// Determine if this is my message (originated on this device)
Tff7b72val Te6edf3fromLocal Tff7b72= Te6edf3packetDataTb4b4b4.Te6edf3isFromLocalTb4b4b4(Te6edf3myNodeNumTb4b4b4)
Tff7b72val Te6edf3toBroadcast Tff7b72= Te6edf3packetDataTb4b4b4.Te6edf3isBroadcast
T8b949e// grab usernames from NodeInfo
Tff7b72val Te6edf3userId Tff7b72= Tff7b72if Tb4b4b4(Te6edf3fromLocalTb4b4b4) Te6edf3packetDataTb4b4b4.Te6edf3to Tff7b72else Te6edf3packetDataTb4b4b4.Te6edf3from
Tff7b72val Te6edf3user Tff7b72= Te6edf3nodeRepositoryTb4b4b4.Te6edf3getUserTb4b4b4(Te6edf3userId Tff7b72?: Te6edf3NodeAddressTb4b4b4.Te6edf3ID_BROADCASTTb4b4b4)
Tff7b72val Te6edf3node Tff7b72= Te6edf3nodeRepositoryTb4b4b4.Te6edf3getNodeTb4b4b4(Te6edf3userId Tff7b72?: Te6edf3NodeAddressTb4b4b4.Te6edf3ID_BROADCASTTb4b4b4)
Tff7b72val Te6edf3shortName Tff7b72= Te6edf3userTb4b4b4.Te6edf3short_name
Tff7b72val Te6edf3longName Tff7b72=
Tff7b72if Tb4b4b4(Te6edf3toBroadcastTb4b4b4) Tb4b4b4{
Te6edf3channelSetTb4b4b4.Te6edf3getChannelTb4b4b4(Te6edf3packetDataTb4b4b4.Te6edf3channelTb4b4b4)Tff7b72?.Te6edf3name Tff7b72?: Ta5d6ff"Ta5d6ffChannel Tffd700${Te6edf3packetDataTb4b4b4.Te6edf3channelTffd700}Ta5d6ff"
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3userTb4b4b4.Te6edf3long_name
Tb4b4b4}
Te6edf3ContactTb4b4b4(
Te6edf3contactKey Tff7b72= Te6edf3contactKeyTb4b4b4,
Te6edf3shortName Tff7b72= Tff7b72if Tb4b4b4(Te6edf3toBroadcastTb4b4b4) Te6edf3packetDataTb4b4b4.Te6edf3channelTb4b4b4.Te6edf3toStringTb4b4b4(Tb4b4b4) Tff7b72else Te6edf3shortNameTb4b4b4,
Te6edf3longName Tff7b72= Te6edf3longNameTb4b4b4,
Te6edf3lastMessageTime Tff7b72= Tff7b72if Tb4b4b4(Te6edf3packetDataTb4b4b4.Te6edf3time Tff7b72!Tff7b72= T79c0ff0LTb4b4b4) Te6edf3packetDataTb4b4b4.Te6edf3time Tff7b72else Tff7b72nullTb4b4b4,
Te6edf3lastMessageText Tff7b72= Tff7b72if Tb4b4b4(Te6edf3fromLocalTb4b4b4) Te6edf3packetDataTb4b4b4.Te6edf3text Tff7b72else Ta5d6ff"Tffd700$Te6edf3shortNameTa5d6ff: Tffd700${Te6edf3packetDataTb4b4b4.Te6edf3textTffd700}Ta5d6ff"Tb4b4b4,
Te6edf3unreadCount Tff7b72= Te6edf3packetRepositoryTb4b4b4.Te6edf3getUnreadCountTb4b4b4(Te6edf3contactKeyTb4b4b4)Tb4b4b4,
Te6edf3messageCount Tff7b72= Te6edf3packetRepositoryTb4b4b4.Te6edf3getMessageCountTb4b4b4(Te6edf3contactKeyTb4b4b4)Tb4b4b4,
Te6edf3isMuted Tff7b72= Te6edf3settingsTff7b72[Te6edf3contactKeyTff7b72]Tff7b72?.Te6edf3isMuted Tff7b72=Tff7b72= Tff7b72trueTb4b4b4,
Te6edf3isUnmessageable Tff7b72= Te6edf3userTb4b4b4.Te6edf3is_unmessagable Tff7b72?: Tff7b72falseTb4b4b4,
Te6edf3nodeColors Tff7b72=
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3toBroadcastTb4b4b4) Tb4b4b4{
Te6edf3nodeTb4b4b4.Te6edf3colors
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72null
Tb4b4b4}Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4.Te6edf3stateInWhileSubscribedTb4b4b4(Te6edf3initialValue Tff7b72= Te6edf3emptyListTb4b4b4(Tb4b4b4)Tb4b4b4)
Tff7b72fun Td2a8ffgetNodeTb4b4b4(Te6edf3userIdTb4b4b4: Tffa657String?Tb4b4b4) Tff7b72= Te6edf3nodeRepositoryTb4b4b4.Te6edf3getNodeTb4b4b4(Te6edf3userId Tff7b72?: Te6edf3NodeAddressTb4b4b4.Te6edf3ID_BROADCASTTb4b4b4)
Tff7b72fun Td2a8ffdeleteContactsTb4b4b4(Te6edf3contactsTb4b4b4: Te6edf3ListTff7b72<Tffa657StringTff7b72>Tb4b4b4) Tff7b72=
Te6edf3safeLaunchTb4b4b4(Te6edf3context Tff7b72= Te6edf3ioDispatcherTb4b4b4, Te6edf3tag Tff7b72= Ta5d6ff"Ta5d6ffdeleteContactsTa5d6ff"Tb4b4b4) Tb4b4b4{ Te6edf3packetRepositoryTb4b4b4.Te6edf3deleteContactsTb4b4b4(Te6edf3contactsTb4b4b4) Tb4b4b4}
Tff7b72fun Td2a8ffmarkAllAsReadTb4b4b4(Tb4b4b4) Tff7b72=
Te6edf3safeLaunchTb4b4b4(Te6edf3context Tff7b72= Te6edf3ioDispatcherTb4b4b4, Te6edf3tag Tff7b72= Ta5d6ff"Ta5d6ffmarkAllAsReadTa5d6ff"Tb4b4b4) Tb4b4b4{ Te6edf3packetRepositoryTb4b4b4.Te6edf3clearAllUnreadCountsTb4b4b4(Tb4b4b4) Tb4b4b4}
Tff7b72fun Td2a8ffsetMuteUntilTb4b4b4(Te6edf3contactsTb4b4b4: Te6edf3ListTff7b72<Tffa657StringTff7b72>Tb4b4b4, Te6edf3untilTb4b4b4: Tffa657LongTb4b4b4) Tff7b72=
Te6edf3safeLaunchTb4b4b4(Te6edf3context Tff7b72= Te6edf3ioDispatcherTb4b4b4, Te6edf3tag Tff7b72= Ta5d6ff"Ta5d6ffsetMuteUntilTa5d6ff"Tb4b4b4) Tb4b4b4{ Te6edf3packetRepositoryTb4b4b4.Te6edf3setMuteUntilTb4b4b4(Te6edf3contactsTb4b4b4, Te6edf3untilTb4b4b4) Tb4b4b4}
Tff7b72fun Td2a8ffgetContactSettingsTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3packetRepositoryTb4b4b4.Te6edf3getContactSettingsTb4b4b4(Tb4b4b4)
Tff7b72fun Td2a8ffsetContactFilteringDisabledTb4b4b4(Te6edf3contactKeyTb4b4b4: Tffa657StringTb4b4b4, Te6edf3disabledTb4b4b4: Tffa657BooleanTb4b4b4) Tb4b4b4{
Te6edf3safeLaunchTb4b4b4(Te6edf3context Tff7b72= Te6edf3ioDispatcherTb4b4b4, Te6edf3tag Tff7b72= Ta5d6ff"Ta5d6ffsetContactFilteringDisabledTa5d6ff"Tb4b4b4) Tb4b4b4{
Te6edf3packetRepositoryTb4b4b4.Te6edf3setContactFilteringDisabledTb4b4b4(Te6edf3contactKeyTb4b4b4, Te6edf3disabledTb4b4b4)
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Get the total message count for a list of contact keys. This queries the repository directly, so it works even if
* a contact isn't currently loaded in the list.
*/
Tff7b72suspend Tff7b72fun Td2a8ffgetTotalMessageCountTb4b4b4(Te6edf3contactKeysTb4b4b4: Te6edf3ListTff7b72<Tffa657StringTff7b72>Tb4b4b4)Tb4b4b4: Tffa657Int Tff7b72= Tff7b72if Tb4b4b4(Te6edf3contactKeysTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
T79c0ff0
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3contactKeysTb4b4b4.Te6edf3sumOf Tb4b4b4{ Te6edf3contactKey Tff7b72-Tff7b72> Te6edf3packetRepositoryTb4b4b4.Te6edf3getMessageCountTb4b4b4(Te6edf3contactKeyTb4b4b4) Tb4b4b4}
Tb4b4b4}
Tff7b72companion Tff7b72object Tb4b4b4{
Tff7b72private Tff7b72const Tff7b72val Te6edf3KEY_COLLAPSED_SECTIONS Tff7b72= Ta5d6ff"Ta5d6ffcollapsed_contact_sectionsTa5d6ff"
Tb4b4b4}
Tb4b4b4}
Tff7b72private Tff7b72fun Te6edf3StringTb4b4b4.Td2a8fftoSectionSetTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3SetTff7b72<Tffa657StringTff7b72> Tff7b72= Te6edf3splitTb4b4b4(Ta5d6ff"Ta5d6ff,Ta5d6ff"Tb4b4b4)Tb4b4b4.Te6edf3filter Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3isNotEmptyTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3toSetTb4b4b4(Tb4b4b4)
T8b949e/** The two conversation-list sections a [Contact] is grouped into. */
Tff7b72enum Tff7b72class T56d364ContactSectionTb4b4b4(Tff7b72val Te6edf3keyTb4b4b4: Tffa657StringTb4b4b4) Tb4b4b4{
Te6edf3CHANNELSTb4b4b4(Ta5d6ff"Ta5d6ffchannelsTa5d6ff"Tb4b4b4)Tb4b4b4,
Te6edf3DIRECT_MESSAGESTb4b4b4(Ta5d6ff"Ta5d6ffdirect_messagesTa5d6ff"Tb4b4b4)Tb4b4b4,
Tb4b4b4}
T8b949e/** Channel/broadcast contact keys carry a leading channel-digit prefix (e.g. `"0^all"`); DM keys don't. */
Tff7b72fun Te6edf3ContactTb4b4b4.Td2a8ffsectionTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3ContactSection Tff7b72=
Tff7b72if Tb4b4b4(Te6edf3contactKeyTb4b4b4.Te6edf3getOrNullTb4b4b4(T79c0ff1Tb4b4b4) Tff7b72=Tff7b72= Ta5d6ff'^' Tff7b72|Tff7b72| Te6edf3contactKeyTb4b4b4.Te6edf3endsWithTb4b4b4(Ta5d6ff"Ta5d6ff^allTa5d6ff"Tb4b4b4) Tff7b72|Tff7b72| Te6edf3contactKeyTb4b4b4.Te6edf3endsWithTb4b4b4(Ta5d6ff"Ta5d6ff^broadcastTa5d6ff"Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3ContactSectionTb4b4b4.Te6edf3CHANNELS
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3ContactSectionTb4b4b4.Te6edf3DIRECT_MESSAGES
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.08s